//
//  UIToogleButton.m
//  Dolphin
//
//  Created by Handymood on 11-6-12.
//  Copyright 2011 __Hanydmood__. All rights reserved.
//

#import "UIToogleButton.h"


@implementation UIToogleButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {

    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)buttonInit:(NSString *)playButtonPath
   inPauseBtnPath:(NSString *) pauseButtonPath
              autoPlay:(BOOL) inAutoPlay
{
    NSString *fileNamePrefix_Play = [playButtonPath  substringWithRange:NSMakeRange(0, [playButtonPath  length] - 4)];
    NSString *fileNameSuffix_Play = [playButtonPath  substringWithRange:NSMakeRange([playButtonPath length]-3,3)];
    
    NSString *fileNamePrefix_Pause = [pauseButtonPath  substringWithRange:NSMakeRange(0, [pauseButtonPath  length] - 4)];
    NSString *fileNameSuffix_Pause = [pauseButtonPath  substringWithRange:NSMakeRange([pauseButtonPath length]-3,3)];
    
        playButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
        pauseButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
        
        [playButton addButtonFace:fileNamePrefix_Play normalImgSuffix:fileNameSuffix_Play downImgPrefix:fileNamePrefix_Play downImgSuffix:fileNameSuffix_Play];
        [playButton addTarget:self action:@selector(mouseDownProcPlay:) forControlEvents:UIControlEventTouchDown];
        
        [pauseButton addButtonFace:fileNamePrefix_Pause normalImgSuffix:fileNameSuffix_Pause downImgPrefix:fileNamePrefix_Pause downImgSuffix:fileNameSuffix_Pause];
        [pauseButton addTarget:self action:@selector(mouseDownProcPause:) forControlEvents:UIControlEventTouchDown];
        
        [self addSubview:playButton];
        [self addSubview:pauseButton];
        
        //set frame for interactive
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 
                                playButton.frame.size.width, playButton.frame.size.height);
        
        playButton.hidden = TRUE;
        pauseButton.hidden = TRUE;
        
        if(inAutoPlay)
        {
            pauseButton.hidden = FALSE;
        }
        else
        {
            playButton.hidden = FALSE;
        }
}

-(void) mouseDownProcPlay:(id)sender
{
    pauseButton.hidden = FALSE;
    playButton.hidden = TRUE;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"slide_show_play" object:self];
}

-(void) mouseDownProcPause:(id)sender
{
    pauseButton.hidden = YES;
    playButton.hidden = FALSE;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"slide_show_pause" object:self];
}

-(void)setXY:(CGFloat) inx 
        offy:(CGFloat) iny
{
    self.frame = CGRectMake(inx, iny, self.frame.size.width, self.frame.size.height);
}

-(void) buttonSizeAdjust:(CGFloat) inwidth
                            height:(CGFloat) inheight
{
    if (pauseButton == Nil) 
    {
        return;
    }
    
    pauseButton.frame = CGRectMake(pauseButton.frame.origin.x,pauseButton.frame.origin.y,inwidth,inheight);
    playButton.frame = CGRectMake(playButton.frame.origin.x,playButton.frame.origin.y,inwidth,inheight);
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, inwidth, inheight);
}

-(BOOL) onPlay
{
    return playButton.hidden;
}

-(void) manuallyPlay
{
    [self mouseDownProcPlay:Nil];
}

-(void) manuallyPause
{
    [self mouseDownProcPause:Nil];  
}

-(void)reset
{
    if(playButton)
    {
        [playButton removeTarget:self action:@selector(mouseDownProcPlay:) forControlEvents:UIControlEventTouchDown];
        [pauseButton removeTarget:self action:@selector(mouseDownProcPause:) forControlEvents:UIControlEventTouchDown];    
        
        [playButton setUserInteractionEnabled:NO];
        [pauseButton setUserInteractionEnabled:NO];
        
        [playButton removeFromSuperview];
        [pauseButton removeFromSuperview];  
    }
}

- (void)dealloc
{
    [super dealloc];
}

@end
